package com.easytake.emotioncat.controller;

import com.easytake.emotioncat.dto.PageResult;
import com.easytake.emotioncat.dto.Response;
import com.easytake.emotioncat.entity.EmListEntity;
import com.easytake.emotioncat.entity.EmUserInfoEntity;
import com.easytake.emotioncat.fileio.FileUtil;
import com.easytake.emotioncat.fileio.minio.MinioConfig;
import com.easytake.emotioncat.param.EmParams;
import com.easytake.emotioncat.service.IEmDictionaryService;
import com.easytake.emotioncat.service.IEmListService;
import com.easytake.emotioncat.service.IEmUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 情绪记录表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-10-23
 */
@Tag(name = "日记接口")
@RestController
@RequestMapping("/em")
//@RequireToken
public class EmListController {
    @Autowired
    IEmListService emListService;

    @Autowired
    IEmUserInfoService userInfoService;

    @Autowired
    FileUtil fileUtil;

    @Autowired
    MinioConfig minioConfig;

    @Autowired
    IEmDictionaryService emDictionaryService;

    @Operation(summary = "发布日记")
    @PostMapping()
    public Response<EmListEntity> save(@RequestHeader String wx_uid, @Validated EmParams em, MultipartFile[] files) {
        if (!emDictionaryService.exist(em.getCategory(), 1)) {
            throw new IllegalArgumentException("情绪类型不存在");
        }
        if (!emDictionaryService.exist(em.getAffect(), 2)) {
            throw new IllegalArgumentException("情绪影响不存在");
        }
        EmUserInfoEntity userInfoEntity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(userInfoEntity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        EmListEntity entity = new EmListEntity();
        BeanUtils.copyProperties(em, entity);
        if (Objects.nonNull(files) && minioConfig.getEnable()) {
            String path = fileUtil.upload(files);
            entity.setImages(path);
        }
        entity.setUserId(wx_uid);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setModifyTime(now);
        entity.setDeleted(0);
        emListService.save(entity);
        return Response.success(entity);
    }

    @Operation(summary = "删除日记")
    @DeleteMapping
    @Parameter(name = "emId", description = "日记id")
    public Response delete(@RequestHeader String wx_uid, @RequestParam @NotNull(message = "删除日记id不能为空") String emId) {
        EmListEntity em = emListService.getByUserAndId(wx_uid, emId);
        if (Objects.nonNull(em)) {
            emListService.removeById(emId);
            if (Objects.nonNull(em.getImages()) && minioConfig.getEnable()) {
                fileUtil.remove(em.getImages());
            }
        }
        return Response.success();
    }


    @Operation(summary = "分页查询日记")
    @GetMapping
    @Parameter(name = "pageNo", description = "页码")
    @Parameter(name = "pageSize", description = "每页显示数量")
    public Response<PageResult<EmListEntity>> getList(@RequestHeader String wx_uid, @RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        PageResult<EmListEntity> emPageResult = emListService.getListByPage(wx_uid, pageNo, pageSize);
        List<EmListEntity> entityList = emPageResult.getResults();
        if (!minioConfig.getEnable()) {
            return Response.success(emPageResult);
        }
        //补充图片完整路径
        for (EmListEntity entity : entityList) {
            String images = entity.getImages();
            if (StringUtils.isNotBlank(images)) {
                String fullPathImages = Arrays.stream(images.split(";")).map(i -> fileUtil.generateFileUrl(i)).collect(Collectors.joining(";"));
                entity.setImages(fullPathImages);
            }
        }
        return Response.success(emPageResult);
    }
}
